//
//  FilterBrandChild2.swift
//  koolcar
//
//  Created by xiantian on 2023/11/28.
//

import UIKit
import TTIndexBar

typealias FilterBrandCarSeriesBlock = (KatashikiListData?, String)->Void
class FilterBrandCarSeries: UIView {
    var selectCarSeriesBlock: FilterBrandCarSeriesBlock?
    var selectBrand: KatashikiListData? {
        didSet {
            //self.selectBrandLab.text = self.selectBrand?.brandName ?? ""
            self.dataArray = self.selectBrand?.katashiki ?? []
        }
    }
    private var dataArray: [String] = []
    var tableView: BaseTableView!
    var selectBrandBtn: UIButton!
    override init(frame: CGRect) {
        super.init(frame: frame)
        let tempHeader = UIView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 40))
        selectBrandBtn = UIButton()
        selectBrandBtn.setup_titleColor(color: Theme.mainColor)
        selectBrandBtn.backgroundColor = UIColor.clear
        selectBrandBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        selectBrandBtn.contentHorizontalAlignment = .left
        selectBrandBtn.addTarget(self, action: #selector(selectBrandBtnAction), for: .touchUpInside)
        selectBrandBtn.setup_titleLabel(text: MMLocalizedString("all_series", comment: "全部车系"))
        tempHeader.addSubview(selectBrandBtn)
        tempHeader.buttomBorder(width: 0.5, borderColor: Theme.lineColor)
        self.addSubview(tempHeader)
        
        tableView = BaseTableView.init(frame: CGRect.zero, style: .plain)
        tableView.register(FilterBrandViewCell.self, forCellReuseIdentifier: "FilterBrandViewCell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        self.addSubview(tableView)
        self.addSnpLayout()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addSnpLayout(){
       
        selectBrandBtn.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.bottom.equalTo(0)
            make.right.equalTo(-14)
        }
        
        tableView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(40)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        }
        
    }
    
    @objc func selectBrandBtnAction(){
        self.selectCarSeriesBlock?(self.selectBrand, "")
    }
}

extension FilterBrandCarSeries: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FilterBrandViewCell", for: indexPath) as! FilterBrandViewCell
        if self.dataArray.count > indexPath.section {
            let series = self.dataArray[indexPath.row]
            cell.titleLab.text = series
        }
        cell.selectionStyle = .default
        cell.clipsToBounds = true
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if self.dataArray.count > indexPath.row {
            let series = self.dataArray[indexPath.row]
            self.selectCarSeriesBlock?(self.selectBrand, series)
        }
    }
}
